### Conn, Lovison, and Mo (2021)
### Create R Figures
### S1: responserate.jpeg
### S2: FirstStage_CM.jpeg

### S15: careers.pdf

rm(list=ls()) 
ssc.install("gplots")
library(ggplot2)
library(tidyverse)
library(readstata13)
library(dplyr)

setwd("Additional Analyses")

tfa_data <- read.dta13('TFA_merged.dta')

#### CREATE Figure S.1

plot_finished_data <-
  tfa_data %>% 
  filter(abs(zscore) < 0.5) %>%
  mutate(treat = (zscore > 0)*1) %>%
  mutate(bins = cut(zscore, breaks = seq(from = (0-0.5), to = (0+0.5), by=.015), dig.lab = 6)) %>%
  group_by(bins) %>%
  mutate(n_obs = n()) %>%
  mutate(outcome_binned = mean(finished, na.rm = T)) %>%
  mutate(bins_split = bins,
         bins_split = str_replace_all(bins_split, "\\(|\\)|\\[|\\]", "")) %>%
  separate(bins_split, into = c("start_bin", "end_bin"), sep = ",") %>%
  mutate_at(vars(start_bin, end_bin), funs(as.numeric(.))) %>%
  mutate(bin_position = (start_bin + end_bin)/2)

bin_df = 
  plot_finished_data %>% 
  select(bin_position, outcome_binned, n_obs) %>% 
  distinct() %>% 
  mutate(treat = NA_real_)

plot_finished =
  plot_finished_data %>%
  ggplot(aes(x = zscore, y = finished, group = treat)) + 
  geom_vline(aes(xintercept = 0), color = 'black', size = 0.5, linetype = 'dashed') + 
  geom_smooth(method = "lm", color = "black",size = 0.4, fill = "transparent") +
  theme_bw() +
  geom_point(data = bin_df, aes(x=bin_position, y = outcome_binned, size = n_obs), color = "#756bb1", fill = alpha("#bcbddc", 0.4), shape = 21, stroke = 1.25) +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        axis.text=element_text(size=16),
        axis.title=element_text(size=16),
        axis.line = element_line(colour = "black"),
        panel.border = element_blank()) +
  xlab("TFA Admissions Score") +
  ylab("Response Rate") +
  coord_cartesian(ylim = c(0, .4)) +
  # geom_rug(sides="b") +
  theme(legend.position = "none")

print(plot_finished)
ggsave("responserate.pdf")

#### create Figure S.2 
plot_matriculate_data <-
  tfa_data %>% 
  filter(abs(zscore) < 0.5) %>%
  mutate(treat = (zscore > 0)*1) %>%
  mutate(bins = cut(zscore, breaks = seq(from = (0-0.5), to = (0+0.5), by=.015), dig.lab = 6)) %>%
  group_by(bins) %>%
  mutate(n_obs = n()) %>%
  mutate(outcome_binned = mean(matriculate, na.rm = T)) %>%
  mutate(bins_split = bins,
         bins_split = str_replace_all(bins_split, "\\(|\\)|\\[|\\]", "")) %>%
  separate(bins_split, into = c("start_bin", "end_bin"), sep = ",") %>%
  mutate_at(vars(start_bin, end_bin), funs(as.numeric(.))) %>%
  mutate(bin_position = (start_bin + end_bin)/2)

bin_df = 
  plot_matriculate_data %>% 
  select(bin_position, outcome_binned, n_obs) %>% 
  distinct() %>% 
  mutate(treat = NA_real_)

plot_matriculate =
  plot_matriculate_data %>%
  ggplot(aes(x = zscore, y = matriculate, group = treat)) + 
  geom_vline(aes(xintercept = 0), color = 'black', size = 0.5, linetype = 'dashed') + 
  geom_smooth(method = "lm", color = "black",size = 0.4, fill = "transparent") +
  theme_bw() +
  geom_point(data = bin_df, aes(x=bin_position, y = outcome_binned, size = n_obs), color = "#756bb1", fill = alpha("#bcbddc", 0.4), shape = 21, stroke = 1.25) +
  theme(panel.grid.minor = element_blank(), 
        panel.grid.major.x = element_blank(),
        axis.line.y.left = element_blank(),
        axis.line = element_line(colour = "black"),
        axis.text=element_text(size=16),
        axis.title=element_text(size=16),
        panel.border = element_blank()) +
  xlab("TFA Admissions Score") +
  ylab("Matriculation Rate") +
  coord_cartesian(ylim = c(0, 1)) +
  # geom_rug(sides="b") +
  theme(legend.position = "none")

print(plot_matriculate)
ggsave("FirstStage.pdf")


